package cn.edu.ccibe.exam16system.controller;

import cn.edu.ccibe.exam16system.CommonResult;
import cn.edu.ccibe.exam16system.entity.Student;
import cn.edu.ccibe.exam16system.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentRegController {

    @Resource
    private StudentService studentService;

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/doRegister")
    @ResponseBody
    public CommonResult<Object> doRegister(Student student) {
        student.setId(null);
        try {
            boolean save = studentService.save(student);
            if (save) return CommonResult.success();
            return CommonResult.fail(500, "注册失败");
        } catch (Exception e) {
            return CommonResult.fail(500, "注册失败");
        }
    }
}
